package com.zxl.designpattern.behavioral.state;

/**
 * 具体状态类：已售出状态
 * 当贩卖机已售出商品时的状态
 */
public class SoldState implements State {

    // 贩卖机
    private final VendingMachine vendingMachine;
    
    /**
     * 构造函数，初始化贩卖机引用
     * @param vendingMachine 贩卖机对象
     */
    public SoldState(VendingMachine vendingMachine) {
        this.vendingMachine = vendingMachine;
    }
    
    @Override
    public void pay(int amount) {
        System.out.println("正在出货，请稍等...");
    }
    
    @Override
    public void selectItem() {
        System.out.println("正在出货，请稍等...");
    }
    
    @Override
    public void dispense() {
        // 发放商品
        vendingMachine.releaseItem();

        // 检查库存
        if (vendingMachine.getCount() > 0) {
            vendingMachine.setState(vendingMachine.getSelectionState());
            System.out.println("交易完成，请选择新商品");
        } else {
            vendingMachine.setState(vendingMachine.getSoldOutState());
            System.out.println("交易完成，商品已售罄");
        }
    }
    
    @Override
    public void refill(int quantity) {
        System.out.println("商品未售罄");
    }
}    